package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gson.reflect.TypeToken;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.CityListAdapter;
import com.iexbuy.ihg.adapter.SearchHistoryListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.CityObject;
import com.iexbuy.ihg.entity.CityObjectResponce;
import com.iexbuy.ihg.entity.SearchHistory;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.YunItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class CityChooseFragment extends BaseFragment {

	private static CityChooseFragment homeFragmentInstent;

	@ViewInject(R.id.listView)
	private ListView listView;

	private CityListAdapter cityListAdapter;
	private List<CityObject> cityObjects = new ArrayList<CityObject>();

	private static List<onCitySelectedListener> listeners = new ArrayList<onCitySelectedListener>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_city_list, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.city_choose));

		initListData();
	}

	private void initListData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CITYS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						CityObjectResponce rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, CityObjectResponce.class);
						cityObjects.clear();
						cityObjects.addAll(rsp.getBody());
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initListView() {
		if (cityListAdapter == null) {
			cityListAdapter = new CityListAdapter(mBaseActivity, cityObjects);
		}
		listView.setAdapter(cityListAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				App.sPreferences.setCityName(cityObjects.get(i).getCityName());
				App.sPreferences.setCityCode(cityObjects.get(i).getCityCode());
				for (onCitySelectedListener listener : listeners) {
					listener.onCitySelected(cityObjects.get(i));
				}
				mBaseActivity.finish();
			}
		});
		cityListAdapter.notifyDataSetChanged();

	}

	public void onResume() {
		super.onResume();
	}

	public interface onCitySelectedListener{
		public void onCitySelected(CityObject cityObject);
	}

	public static void addOnCitySelectedListener(onCitySelectedListener listener) {
		if (!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static CityChooseFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
